package com.itbour.web.action.orders;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.log.ErrorLogger;
import com.itbour.common.util.StringUtils;
import com.itbour.def.MessageCode;
import com.itbour.def.MessageCodeConstants;
import com.itbour.def.MessageType;
import com.itbour.def.SystemConf;
import com.itbour.dto.goods.AttractionsDto;
import com.itbour.dto.goods.ResortDto;
import com.itbour.service.goods.GoodsService;
import com.itbour.service.orders.OrdersService;
import com.opensymphony.xwork2.ActionContext;

/**
 * copyright pirate alliance team 2014
 *
 * @author songyong
 */

public class GetOrdersListWebAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private OrdersService ordersService;
	private List<ResortDto> goodsList;
	private ResortDto resortDto;
	private String id;
	
	public void ordersList() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		Map<String,Object> param = new HashMap<String,Object>(); 
		String id = request.getParameter("id");
		if(StringUtils.isNotEmpty(id)){
			param.put("id", id);
		}
		String username = request.getParameter("username");
		if(StringUtils.isNotEmpty(username)){
			param.put("username", username);
		}
		String resortname = request.getParameter("resortname");
		if(StringUtils.isNotEmpty(resortname)){
			param.put("resortname", resortname);
		}
		Page page =ordersService.pageQuery(param, Integer.parseInt(pageNumber), Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		writeJSON(resMap);
	}
	
	public String goodsDetail(){
		long id = Long.valueOf(ServletActionContext.getRequest().getParameter("id"));
		//this.resortDto=this.ordersService.getGoodsDetailById(id);
		return SUCCESS;
	}
	public List<ResortDto> getGoodsList() {
		return goodsList;
	}
	public void setGoodsList(List<ResortDto> goodsList) {
		this.goodsList = goodsList;
	}
	public ResortDto getResortDto() {
		return resortDto;
	}
	public void setResortDto(ResortDto resortDto) {
		this.resortDto = resortDto;
	}
	
	public OrdersService getOrdersService() {
		return ordersService;
	}

	public void setOrdersService(OrdersService ordersService) {
		this.ordersService = ordersService;
	}

	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * 图像保存
	 * @return
	 */
	private String saveImageTemp(File image)
	{
		String imageRootDir = SystemConf.getImageRootDir();
		Date date = new Date();
		DateFormat df1 = new SimpleDateFormat("yyyyMMdd");
		DateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmss");
		String tmpImagePath = "temp/" + df1.format(date)+ "/" + df2.format(date) + ".jpg";
  
		String fileUrl = StringUtils.linkFilePath(imageRootDir, tmpImagePath);
		File saveFile = new File(fileUrl);

		if(image == null)
		{
			return "";
		}
		try
		{
			FileUtils.copyFile(image, saveFile);
		}
		catch (IOException e)
		{
			ErrorLogger.log(e, new MessageCode(MessageCodeConstants.E_00000001,MessageType.ERROR), "RegisterAction", e.getMessage());
			return null;
		}
		finally
		{
			image.delete();
		}

		return tmpImagePath;
	}
}
